package jwei.apps;

import java.util.List;

import jwei.apps.db.StyleDb;
import jwei.apps.objects.PurchasedStyle;
import jwei.apps.zappos.ZapposGetLatest;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private final String LOG_TAG = "MainActivity";

    private Button search, queue;

    private TextView latestPurchaseText;

    private Handler handler = new Handler();

    private List<PurchasedStyle> styles;

    private int index = 0;

    private PurchasedStyle curStyle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        search = (Button) findViewById(R.id.search_zappos);
        queue = (Button) findViewById(R.id.user_queue);
        latestPurchaseText = (TextView) findViewById(R.id.latest_purchase_text);

        search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SearchZapposActivity.class));
            }

        });

        queue.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ShoppingCartActivity.class));
            }

        });

        latestPurchaseText.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, ProductDetailsActivity.class);
                long productId = curStyle.getProductId();

                StyleDb sdb = StyleDb.getInstance();
                sdb.addStyle(getContentResolver(), curStyle);

                i.putExtra("productId", productId);
                i.putExtra("productName", curStyle.getProductName());
                i.putExtra("productUrl", curStyle.getProductUrl());
                i.putExtra("brandName", curStyle.getBrandName());
                i.putExtra("imageUrl", curStyle.getImageUrl());
                i.putExtra("styleId", curStyle.getStyleId());
                i.putExtra("price", curStyle.getPrice());
                startActivity(i);
            }

        });

        new GetZapposLatest().execute();
    }

    private class GetZapposLatest extends AsyncTask<String, String, List<PurchasedStyle>> {

        @Override
        protected void onPreExecute() {
            Log.i(LOG_TAG, "Attempting to retrieve latest Zappos purchases...");
        }

        protected List<PurchasedStyle> doInBackground(String... param) {
            List<PurchasedStyle> ret = ZapposGetLatest.getLatestPurchases();
            return ret;
        }

        protected void onPostExecute(List<PurchasedStyle> results) {
            // scroll these results in the footer
            if (results.size() > 0) {
                styles = results;
                handler.postDelayed(updateStatus, 100);
            }
        }
    }

    private Runnable updateStatus = new Runnable() {

        @Override
        public void run() {
            updateFooterText();
            // 8 second delay
            handler.postDelayed(this, 8 * 1000);
        }

    };

    private void updateFooterText() {
        if (styles != null && styles.size() > 0) {
            int i = index++ % styles.size();
            // get current style
            curStyle = styles.get(i);
            // build message
            StringBuilder sb = new StringBuilder();
            sb.append("Someone in ");
            sb.append(curStyle.getState() + " ");
            sb.append("just bought a ");
            sb.append("<b>" + curStyle.getProductName() + "</b> ");
            sb.append("by <b>");
            sb.append(curStyle.getBrandName() + "</b>!");
            sb.append(" Click for more details!");
            latestPurchaseText.setText(Html.fromHtml(sb.toString()));
        } else {
            Log.e(LOG_TAG, "Error updating latest footer text");
        }
    }
}
